﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Model.CSP;
using CSP.Common.Factories;
using CSP.Dao;

namespace CSP.Bll.ObjectBlls
{
    public class UserCreditBll : CSPCommonBll, IUserCreditBll
    {
        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEY = "UserCredit_";

        private IUserCreditDao _userCreditDao;

        public UserCreditBll()
        {
            _cacheProvider = CachedProviderFactory.GetMemCachedProvider();
            _userCreditDao = CSPDaoFactory.GetInstance().GetUserCreditDao();
        }

        public UserCredit GetByUser(string userName)
        {
            string cacheKey = CACHEKEY + userName;
            if (userName == "")
            {
                throw new Exception("userName can't be null");
            }
            UserCredit userCredit = _cacheProvider.GetData<UserCredit>(cacheKey);
            if (userCredit == null)
            {
                userCredit = _userCreditDao.GetByUser(userName);
                DateTime failureTime = DateTime.Now.AddHours(1);
                _cacheProvider.AddData(cacheKey, userCredit, failureTime);
            }

            return userCredit;

        }

        public Model.CSP.UserCredit GetDataById(int id)
        {
            throw new NotImplementedException();
        }

        public ICollection<Model.CSP.UserCredit> GeDatatByIds(int[] Ids)
        {
            throw new NotImplementedException();
        }

        public void Add(Model.CSP.UserCredit obj)
        {
            _userCreditDao.Add(obj);
        }

        /// <summary>
        /// 创建用户信誉
        /// </summary>
        /// <param name="userCredit"></param>
        public void Create(UserCredit userCredit)
        {
            _userCreditDao.Add(userCredit);
        }

        public void Update(Model.CSP.UserCredit obj)
        {
            throw new NotImplementedException();
        }

        public void DeleteById(int id)
        {
            throw new NotImplementedException();
        }

        public void RefreshCache()
        {
            throw new NotImplementedException();
        }
    }
}
